/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ipti.br.tag.dao;

import org.ipti.br.tag.entity.Discipline_;
import org.ipti.br.tag.entity.Organization_;
import org.ipti.br.tag.entity.Plan;
import org.ipti.br.tag.entity.Plan_;
import org.ipti.br.tag.entity.Unity_;
import java.util.Calendar;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.ipti.br.tag.util.EncryptionUtil;

/**
 *
 * @author israel
 */
@Stateless
public class PlanFacade extends AbstractFacade<Plan> {

    @PersistenceContext(unitName = "TAG2PU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PlanFacade() {
        super(Plan.class);
    }

    @Override
    public void create(Plan plan) {
        getEntityManager().persist(plan);
    }

    @Override
    public void edit(Plan plan) {
        getEntityManager().merge(plan);
    }

    public List<Plan> findPlanPerOrganizationUnityDisciplineYear(Long organizationID, Long unityID, Long disciplineID, int year) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Plan> cq = cb.createQuery(Plan.class);
        Root<Plan> from = cq.from(Plan.class);
        Predicate predicate1 = cb.equal(from.get(Plan_.organization).get(Organization_.id), organizationID);
        Predicate predicate2 = cb.equal(from.get(Plan_.unity).get(Unity_.id), unityID);
        Predicate predicate3 = cb.equal(from.get(Plan_.discipline).get(Discipline_.id), disciplineID);
        Predicate predicate4 = cb.equal(from.get(Plan_.planYear), year);
        cq.where(cb.and(predicate1, predicate2, predicate3, predicate4));

        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<Plan> findPlanPerOrganizationDisciplineYear(Long organizationID, Long disciplineID, int year) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Plan> cq = cb.createQuery(Plan.class);
        Root<Plan> from = cq.from(Plan.class);
        Predicate predicate1 = cb.equal(from.get(Plan_.organization).get(Organization_.id), organizationID);
        Predicate predicate2 = cb.equal(from.get(Plan_.discipline).get(Discipline_.id), disciplineID);
        Predicate predicate3 = cb.equal(from.get(Plan_.planYear), year);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }

    public List<Plan> modifiedPlanBetweenDate(Calendar initialDate, Calendar finalDate, Long serialNumber) {
        CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
        CriteriaQuery<Plan> cq = cb.createQuery(Plan.class);
        Root<Plan> from = cq.from(Plan.class);
        Predicate predicate1 = cb.notEqual(from.get(Plan_.serialNumber), serialNumber);
        Predicate predicate2 = cb.greaterThanOrEqualTo(from.get(Plan_.lastModified), initialDate);
        Predicate predicate3 = cb.lessThan(from.get(Plan_.lastModified), finalDate);
        cq.where(cb.and(predicate1, predicate2, predicate3));

        return getEntityManager().createQuery(cq).getResultList();
    }
}
